package com.wenbing.miio.core;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 上下文的简单实现
 * @author wen bing
 * @projectName: miio
 * @package: core.ps
 * @className: SContext
 * @date 2024/09/19 12:31
 **/
public class MapContext implements Context {

    private Map<String, Object> attributes;

    public MapContext() {
        this.attributes = new ConcurrentHashMap<>(16);
    }

    @Override
    public void setAttribute(String key, Object attr) {
        if (key == null || attr == null) return;
        this.attributes.put(key, attr);
    }

    @Override
    public <T> T getAttribute(String key) {
        if (key == null) return null;
        return (T) attributes.get(key);
    }

    @Override
    public <T> T getAttribute(String key, T defVal) {
        if (key == null) return defVal;
        return (T) attributes.getOrDefault(key, defVal);
    }

    @Override
    public void removeAttribute(String key) {
        if (key == null) return;
        attributes.remove(key);
    }

    @Override
    public Set<String> keySet() {
        return attributes.keySet();
    }

    @Override
    public String toString() {
        return "SimpleContext{" +
                "attributes=" + attributes +
                '}';
    }


}
